/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is  James Lutsko 
 * (jim@lutsko.com http://www.lutsko.com).
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */


package autoschedule.ui;


import java.util.*;
import autoteam.tables.AutoTeamTableModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

import autoschedule.*;


public class ScheduleTableModel extends AutoTeamTableModel
{

    AutoScheduleDoc doc = null;
    Facility f = null;
    League l = null;
    Team t = null;
    JComboBox box;
    static final String[] columnNames = {"Team1","Team2","Facility","Date","Time" };

    public ScheduleTableModel(AutoScheduleDoc _doc, JComboBox _box)
    {
	doc = _doc;
	box = _box;
    }


    void setFacilityFilter(Facility _f)
    {
	f = _f;
	l = null;
	t = null;
	fireTableDataChanged();
    }

    void setLeagueFilter(League _l)
    {
	f = null;
	l = _l;
	t = null;

	if(l != null)
	    {		
		box.setModel(new DefaultComboBoxModel(l.getAllTeams()));
		for(Iterator I = l.getInterLeagueTeams().iterator();I.hasNext();)
		    box.addItem(I.next());
		box.addItem("InterLeague");
	    }
	
	fireTableDataChanged();
    }

    void setTeamFilter(Team _t)
    {
	f = null;
	l = null;
	t = _t;
	fireTableDataChanged();
    }


    public void add(int j)
    {
	doc.addGame(l);
    }


    public void delete(int r)
    {
	Game g = (Game) getSelectedObject(r);
	doc.deleteGame(g);
    }

    public void setDoc(AutoScheduleDoc _doc)
    {
	doc = _doc;
	fireTableDataChanged();
    }

    public void handleDrop(Object o, int row)
    {
	Game g1 = (Game) o;
	Game g2 = (Game) getSelectedObject(row);
	System.out.println("Switching " + g1 + " with " + g2);

	doc.switchGames(g1,g2);
    }



    public Object getSelectedObject(int row)
    {
	if(row < 0) return null;
	if(row >= getRowCount()) return null;
	return 	doc.getGame(row,f,l,t);
    }


    public int getColumnCount() {return columnNames.length;}
        
    public int getRowCount() {return (doc == null ? 0 : doc.getNumberOfGames(f,l,t));}

    public String getColumnName(int col) {return columnNames[col];}

    public Class getColumnClass(int col) {
	if(col >= 0 && col <=2) return String.class;
	if(col == 3) return Date.class;
	if(col == 4) return Integer.class;
	return null;
    }

    public boolean isCellEditable(int row, int col) {return doc.getGame(row,f,l,t).isInterLeague()
    							 || ((l != null) && (col < 2));}


    public Object getValueAt(int row, int col) {
	Game g = doc.getGame(row,f,l,t);
	if(col == 0)  return g.getTeam(1);
	if(col == 1)  return g.getTeam(2);
	if(col == 2)return g.getFacilityName();
	if(col == 3) return g.getDate();
	if(col == 4) return (g.getTime() == null ? null : new Integer(g.getTime().toInt()));
	return null;
    }


    public void setValueAt(Object value, int row, int col) 
    {
	Game g = doc.getGame(row,f,l,t);
	
	if(col < 2)
	    {
		if(value instanceof String) // create an interleague team
		    {
			String inputValue = JOptionPane.showInputDialog("Please enter the name of the interleague team");
			if(inputValue != null)
			    doc.createInterleagueTeam(inputValue,g,l,col);
		    } else {
			g.setTeam(col,value);
		    }
		box.setModel(new DefaultComboBoxModel(l.getAllTeams()));
		for(Iterator I = l.getInterLeagueTeams().iterator();I.hasNext();)
		    box.addItem(I.next());
		box.addItem("InterLeague");
	    }
	if(col == 2) g.setFacilityString(value.toString());

	if(col == 3) g.setDate((Date) value);
	if(col == 4) g.setTime((Integer) value);
    }


}




